package shop.yaojianfeng.springlearning.section5.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.yaojianfeng.springlearning.common.component.MyException;

import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * xml 方式 进行异常处理
 * @author yaojianfeng
 */
@Controller
@RequestMapping("/exception")
public class ShowExceptionController {

    /**
     * 文件找不到异常 -500状态码
     */
    @RequestMapping("/test")
    public void testException() throws FileNotFoundException {
      throw new FileNotFoundException();
    }

    /**
     * 算术运算异常模拟
     */
    @RequestMapping("/test01")
    public void testException01() throws ArithmeticException {
        System.out.println(5 / 0);
    }

    /**
     * 服务器异常模拟
     */
    @RequestMapping("/test02")
    @ResponseBody
    public int testException02() throws NumberFormatException{
        return Integer.parseInt("nihao");
    }

    /**
     * IO异常模拟
     * @throws IOException
     */
    @RequestMapping("/test03")
    public void testException03() throws IOException {
        throw new IOException();
    }

    /**
     * 自定义异常模拟
     */
    @RequestMapping("/test04")
    public void testException04() throws MyException {
        throw new MyException("myException","这个是由自定义异常处理器来进行统一管理", 100);
    }
}
